// app.js
const api = require("/request/api")
const request = require("/request/request")
const util = require("/utils/util")
App({
  onLaunch() {
    // // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now()) 
    // wx.setStorageSync('logs', logs)
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
    // 登录
    const that = this;
    wx.login({
      success: res => {
        let params = {
          code: res.code
        }
        request.post(api.login, params).then(res => {
          that.globalData.userInfo = res
          that.globalData.eid = res.eid
          that.globalData.etype = res.etype
          that.globalData.eshop_id = res.my_shop_id
          that.globalData.if_init = true
          util.saveAppValue(this.globalData.uid, res.uid);
        })
      },
      fail: res => {
        console.log(res);
      }
    })
  },
  
  globalData: {
    if_init: false,
    userInfo: {},
    if_register: false,
    uid: 'gold_carry_uid',
    eid: '',
    etype: '',
    eshop_id: '',
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
  }
})
